package de.baumann.browser.activitys.card

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.text.Html
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import de.baumann.browser.R
import de.baumann.browser.adapter.CardPowerAdapter
import de.baumann.browser.adapter.PowerDescAdapter
import de.baumann.browser.api.net.vo.PowerDesc
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.IMyCardView
import de.baumann.browser.present.MyCardPresenter
import de.baumann.browser.utils.getUserName
import kotlinx.android.synthetic.main.activity_my_card.*
import kotlinx.android.synthetic.main.include_tool_bar.*

class MyCardActivity : BaseOdinActivity<IMyCardView, MyCardPresenter>(), IMyCardView {

    var powerTextColor = R.color.windowLight

    companion object {
        fun startMyCardActivity(context: Context) {
            context.startActivity(Intent(context, MyCardActivity::class.java))
        }
    }

    override fun getLayout(): Int {
        return R.layout.activity_my_card
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        initToolbar(commToolBar, null)
        cardOwner.text = "尊敬的${getUserName()}"
        rvMyCardPower.layoutManager = LinearLayoutManager(mContext)
        rvMyCardPowerDesc.layoutManager = LinearLayoutManager(mContext)
    }

    override fun initData() {
        presenter.getMyCard()
    }

    override fun createPresenter(): MyCardPresenter {
        return MyCardPresenter()
    }

    override fun createView(): IMyCardView {
        return this
    }

    override fun setLastTime(d: String, h: String, m: String) {
        if (d.toInt() <= 0 && h.toInt() <= 0 && m.toInt() <= 0) {
            lastTime.setTextColor(resources.getColor(R.color.color_C6B177))
            lastTime.text = "正在核实"
            return
        }
        val time = String.format(getString(R.string.time, d, h, m))
        lastTime.text = Html.fromHtml(time)
    }

    override fun setActiveStatus(status: Int) {
        var statusColor = resources.getColor(R.color.windowGrayM)
        var statusText = ""
        when (status) {
            0 -> {
                lastTimeStatus.visibility = View.GONE
                rlLastTime.visibility = View.VISIBLE
            }
            1 -> {
                rlLastTime.visibility = View.GONE
                lastTimeStatus.visibility = View.VISIBLE
                statusColor = resources.getColor(R.color.color_1BCE5F)
                statusText = "恭喜您，奖励算力已发放至您的账号"
                rlHash.visibility = View.GONE
                myCardProgress.visibility = View.GONE
            }
            2 -> {
                lastTimeStatus.visibility = View.VISIBLE
                rlLastTime.visibility = View.GONE
                statusColor = resources.getColor(R.color.color_FF2D2D)
                statusText = "很遗憾，您未在七日内完成算力达标任务"
                rlHash.visibility = View.GONE
                myCardProgress.visibility = View.GONE
            }
        }

        lastTimeStatus.text = statusText
        lastTimeStatus.setTextColor(statusColor)
    }

    override fun setCardType(type: Int) {
        var cardBg = R.drawable.blackcard_bg_black
        var cardTextColor = R.color.windowLight
        var typeName = "黑"
        when (type) {
            0 -> {
                cardBg = R.drawable.blackcard_bg_black
                cardTextColor = R.color.windowLight
                powerTextColor = R.color.color_999999
                typeName = "黑"
            }
            1 -> {
                cardBg = R.drawable.blackcard_bg_gold
                cardTextColor = R.color.color_black_121212
                powerTextColor = R.color.color_666666
                typeName = "金"
            }
            2 -> {
                cardBg = R.drawable.blackcard_bg_white
                cardTextColor = R.color.color_black_121212
                powerTextColor = R.color.color_666666
                typeName = "银"
            }
        }
        rlMyCard.setBackgroundResource(cardBg)
        myCardName.setTextColor(resources.getColor(cardTextColor))
        cardType.text = String.format("您已参加%s卡用户成长计划", typeName)
    }

    @SuppressLint("SetTextI18n")
    override fun setCardName(name: String) {
        myCardName.text = name
        commTitle.text = name
        activePlan.text = name + "活动详情"
        cardType.text = "您已参加${name}"
    }

    override fun setCardPowers(powers: List<String>) {
        rvMyCardPower.adapter = CardPowerAdapter(mContext, powers, powerTextColor)
    }

    override fun setCardPowerDescs(descs: List<PowerDesc>) {
        rvMyCardPowerDesc.adapter = PowerDescAdapter(descs)
    }

    override fun setHash(totalHash: Double, lastHash: Double) {
        if (lastHash <= 0) {
            myCardLastHash.text = "算力已达标"
        } else {
            val ss = SpannableString("$lastHash 算力")
            val fc = ForegroundColorSpan(resources.getColor(R.color.color_8793AC))
            ss.setSpan(fc, ss.length - 2, ss.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
            myCardLastHash.text = ss
        }
        myCardProgress.max = totalHash.toInt()
        val progress = totalHash - lastHash
        setHashProgress(progress.toInt())
    }

    override fun setHashProgress(progress: Int) {
        myCardProgress.progress = progress
    }

}
